home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_05 / test_obj / tutils.c < prev    next >
C/C++ Source or Header  |  1993-01-20  |  20KB  |  517 lines

  1. /* File:     tutils.c
  2.    Copyright Norman Wilde 1993
  3.    Notes:    code for utilities used in testing system
  4. */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include "tutils.h"
  10. /* ----------- buffer for strings, blocks, etc. ------------- */
  11. #define MAXCOLLECT 10000
  12. char collectBuf[MAXCOLLECT] = {'\0'}; /* reserve space for buffer*/
  13. char * collectPos = collectBuf;       /* points to next free space in buffer*/
  14. void collect(char c) {        /* collect char c into the buffer */
  15.   *collectPos++ = c;
  16.   assert(collectPos - collectBuf < MAXCOLLECT); // abort if buf overflows
  17. }
  18. void collectFirst(char c) {  /* reset buf and collect c */
  19.   collectPos = collectBuf;
  20.   collect(c);
  21. }
  22. char * release() {           /* copy the string in the buf to the
  23.                                 heap, terminate it, and return the copy*/
  24.   char * t = (char *) calloc(sizeof (char), collectPos - collectBuf + 1);
  25.   char * s;
  26.   char * i;
  27.   s = t;
  28.   assert ( t > 0);
  29.   for (i = collectBuf; i < collectPos; )
  30.      *t++ = *i++;
  31.   *t = '\0';
  32.   return s;
  33. }
  34. /* ----------- simple linked list of strings with data---------------- */
  35. struct STRLIST * listNew(char* aStr, void * aData) {
  36.     /* Answer a new list with aStr as only element */
  37.     struct STRLIST *t = (struct STRLIST *) malloc(sizeof(struct STRLIST));
  38.     t->str  = aStr;
  39.     t->data = aData;
  40.     t->next = NULL;
  41.     return t;
  42. }
  43. struct STRLIST * listAdd(struct STRLIST * aList,char* aStr, void * aData) {
  44.     /* Add aStr to aList */
  45.     struct STRLIST *t = (struct STRLIST *) malloc(sizeof(struct STRLIST));
  46.     t->str  = aStr;
  47.     t->data = aData;
  48.     t->next = aList;
  49.     return t;
  50. }
  51. void * listFind(struct STRLIST * aList, char * aStr) {
  52.     /* Locate string matching aStr in aList. Answer the data ptr. if
  53.        found, NULL if not found */
  54.   struct STRLIST * v;
  55.   for(v = aList; v != NULL; v = v->next) {
  56.     if(strcmp(aStr,v->str) ==0) break;
  57.   }
  58.   if(v==NULL)
  59.     return NULL;
  60.   else
  61.     return v->data;
  62. }
  63. int listSize(struct STRLIST *aList) {
  64.     /* Answer the number of elements in the list */
  65.   struct STRLIST * v;
  66.   int size = 0;
  67.   for(v = aList; v != NULL; v = v->next)
  68.     size++;
  69.   return size;
  70. }
  71. /* --- routines to create the test driver program ------- */
  72. #define FBUFSIZE 10000                   /*size of some internal bufs */
  73. static char Buffer[FBUFSIZE];            /*assemble strings as needed */
  74. static char r_name[21] = {'\0'};         /*name given in the runtest
  75.                        stmt currently being processed*/
  76. static char * MainDecls = NULL;          /*store on heap the declarations in
  77.                        the main fcn being built */
  78. static char * MainStmts = NULL;          /*store on heap the statements in
  79.                        the main fcn beint built */
  80. static void addMainDecls(char * decls) {
  81.    /* append to MainDecls a copy of the string in decls */
  82.    char * temp;
  83.    if(MainDecls == NULL) {
  84.        MainDecls = calloc(strlen(decls) + 1, sizeof(char));
  85.        strcpy(MainDecls, decls);
  86.        return;
  87.    } else {
  88.        temp = calloc(strlen(decls) + strlen(MainDecls) + 1, sizeof(char));
  89.        strcpy(temp, MainDecls);
  90.        strcat(temp, decls);
  91.        free(MainDecls);
  92.        MainDecls = temp;
  93.        return;
  94.    }
  95. }
  96. static void addMainStmts(char * stmts) {
  97.    /* append to MainStmts a copy of the string in stmts */
  98.    char * temp;
  99.    if(MainStmts == NULL) {
  100.        MainStmts = calloc(strlen(stmts) + 1, sizeof(char));
  101.        strcpy(MainStmts, stmts);
  102.        return;
  103.    } else {
  104.        temp = calloc(strlen(stmts) + strlen(MainStmts) + 1, sizeof(char));
  105.        strcpy(temp, MainStmts);
  106.        strcat(temp, stmts);
  107.        free(MainStmts);
  108.        MainStmts = temp;
  109.        return;
  110.    }
  111. }
  112. void addBlock(char * block) {
  113.    /* add to the driver the given embedded block */
  114.    /* remove first '{' and last '}' */
  115.    char * tmpStr = calloc(strlen(block), sizeof(char));
  116.    strncat(tmpStr, block + 1, strlen(block) -2);
  117.    printf("%s\n", tmpStr);
  118.    free(tmpStr);
  119. }
  120.  
  121. void setRunName(char * theRName) {
  122.    /* set the name of the test run to the frist 20 chars of theRName*/
  123.    strncpy(r_name, theRName, 20);
  124. }
  125. static char * getArgDeclStr(struct STRLIST *varList) {
  126.    /* Answer a string on the heap with a comma separated list of the
  127.       variables in varList formatted as:
  128.      <argtype> var, <argtype>var,
  129.       suitable for a function's argument declarations.
  130.    */
  131.    char * res, * remainder, * type;
  132.    int thisVarLength, resultLength;
  133.    if(varList == NULL) return NULL;
  134.    type = getVarArgType(varList->str);
  135.    thisVarLength = strlen(varList->str) + strlen(type) + 3;
  136.    if(varList->next == NULL){
  137.      res = calloc(thisVarLength + 1, sizeof (char) );
  138.      sprintf(res, "%s %s", type, varList->str);
  139.      return res;
  140.    }
  141.    remainder = getArgDeclStr( varList->next);
  142.    resultLength = strlen(remainder) + thisVarLength + 1;
  143.    res = calloc(resultLength + 1, sizeof (char) );
  144.    sprintf(res, "%s %s,%s", type, varList->str, remainder);
  145.    free(remainder);
  146.    return res;
  147. }
  148. static char * genFcnString(
  149.    char * run_name,
  150.    char * fcn_name,
  151.    struct STRLIST *varList,
  152.    char* body) {
  153.     /* Answer function string (on heap) for first variable in list*/
  154.   char * declarations;                        /*decls for var. varList->str*/
  155.   int n;                                      /*num. chars assembled */
  156.   char * theFunc;                             /*the result function string*/
  157.   char * remainingVars;                       /*string with vars after this*/
  158.   /* must have at least one variable in the list */
  159.   assert( varList != NULL );
  160.   declarations = getVarDeclStr(varList->str);
  161.   if(varList->next != NULL) {
  162.     remainingVars = getArgDeclStr(varList->next);
  163.     n = sprintf(Buffer,"void %s_%s(int vals[], %s){\n%s%s\n}\n",
  164.         run_name, fcn_name, remainingVars, declarations, body);
  165.     assert(n>0 && n < FBUFSIZE - 2);
  166.     free(remainingVars);
  167.     theFunc = calloc(strlen(Buffer)+1, sizeof(char));
  168.     strcpy(theFunc,Buffer);
  169.   } else {
  170.     n = sprintf(Buffer,"void %s_%s(int vals[]){\n %s%s\n}\n",
  171.         run_name, fcn_name, declarations, body);
  172.     assert(n>0 && n < FBUFSIZE - 2);
  173.     theFunc = calloc(strlen(Buffer)+1, sizeof(char));
  174.     strcpy(theFunc,Buffer);
  175.   }
  176.   return theFunc;
  177. }  
  178. static char * genBottomFcnString(
  179.    char * run_name,
  180.    struct STRLIST *varList,
  181.    char* body) {
  182.     /* Answer function string (on heap) for the 'bottom' function
  183.        that contains the actual test code*/
  184.   int n;                                      /*num. chars assembled */
  185.   char * theFunc;                             /*the result function string*/
  186.   char * remainingVars;                       /*string with vars after this*/
  187.   /* must have at least one variable in the list */
  188.   assert( varList != NULL );
  189.   remainingVars = getArgDeclStr(varList);
  190.   n = sprintf(Buffer,"void %s_bottom(int vals[], %s)\n%s\n",
  191.           run_name, remainingVars, body);
  192.   assert(n>0 && n < FBUFSIZE - 2);
  193.   free(remainingVars);
  194.   theFunc = calloc(strlen(Buffer)+1, sizeof(char));
  195.   strcpy(theFunc,Buffer);
  196.   return theFunc;
  197. }
  198. static char * getVarNameStr(struct STRLIST *varList) {
  199.    /* Answer a string on the heap with a comma separated list of the
  200.       variables in varList. If there are no variables, return
  201.       an empty string.
  202.    */
  203.    char * res, * remainder;
  204.    int thisVarLength, resultLength;
  205.    if(varList == NULL)               /* if no variables, return empty str */
  206.      return calloc(1, sizeof (char) );
  207.    thisVarLength = strlen(varList->str);
  208.    if(varList->next == NULL){
  209.      res = calloc(thisVarLength + 1, sizeof (char) );
  210.      sprintf(res, "%s", varList->str);
  211.      return res;
  212.    }
  213.    remainder = getVarNameStr( varList->next);
  214.    resultLength = strlen(remainder) + thisVarLength + 1;
  215.    res = calloc(resultLength + 1, sizeof (char) );
  216.    sprintf(res, "%s,%s", varList->str, remainder);
  217.    free(remainder);
  218.    return res;
  219. }
  220.  
  221. static char * genArgString(struct STRLIST *varList) {
  222.    /* Answer a string (on heap) of the form:
  223.      <1stvar>[vals[n-1]], <2ndvar>, ...<nthvar>
  224.    */
  225.   int n = list